//
//  joinQuestViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/6.
//

#import "joinQuestViewController.h"
#import "joinQuestListModel.h"
#import "joinScreenViewController.h"
#import "joinQuestTableViewCell.h"
#import "TimeFrameShowView.h"
#import "joinQuestDetailsViewController.h"
#import "ExamineModel.h"

@interface joinQuestViewController ()

///全部
@property (nonatomic ,strong) UIButton * qbBtn;
///转
@property (nonatomic ,strong) UIButton * zjBtn;
@property (nonatomic ,strong) UILabel * zjlab;
///拒接
@property (nonatomic ,strong) UIButton * jsBtn;
@property (nonatomic ,strong) UILabel * jslab;
@property (nonatomic ,strong) UIView * btnline;

///筛选
@property (nonatomic ,strong) UIButton * SXBtn;
@property (nonatomic ,strong) NSMutableArray <joinQuestListModel *> * modelArray;



///获取数量
@property (nonatomic ,strong) NSURLSessionDataTask * task;
@end

@implementation joinQuestViewController
-(NSMutableArray<joinQuestListModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (joinScreenModel *)model{
    if(!_model){
        _model = [joinScreenModel new];
        _model.createTime = [NSDate br_stringFromDate:[NSDate new] dateFormat:@"yyyy-MM"];
    }
    return _model;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshingData];
    self.SXBtn.selected = !self.model.isNew;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.shutDownBack = YES;
    
    UIView * topview = [UIView new];
    [self.view addSubview:topview];
    topview.backgroundColor = KcolorHex(@"#F1F5FA");
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(kwt(50));
    }];
    
    UIButton * qbbtn = [UIButton buttonWithType:0];
    self.qbBtn = qbbtn;
    qbbtn.addTarget(self,@selector(qbBtnClick));
    qbbtn.addNormalText(@"全部").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [topview addSubview:qbbtn];
    [qbbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.offset(0);
        make.width.offset(kwt(80));
    }];
    
    UIButton * zjBtn = [UIButton buttonWithType:0];
    self.zjBtn = zjBtn;
    zjBtn.addTarget(self,@selector(zjBtnClick));
    zjBtn.addNormalText(@"转交").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [topview addSubview:zjBtn];
    [zjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.equalTo(qbbtn.mas_right);
        make.width.offset(kwt(80));
    }];
    
    UILabel * zjlab = [DTKTool labelWithText:@"0" andFont:kCFont(10) andTextColor:UIColor.whiteColor];
    self.zjlab = zjlab;
    zjlab.backgroundColor = KcolorHex(@"#FE5402");
    zjlab.textAlignment = NSTextAlignmentCenter;
    [DTKTool YJ:kwt(9) andView:zjlab];
    [zjBtn addSubview:zjlab];
    [zjlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(11));
        make.centerX.equalTo(zjBtn).offset(kwt(25));
        make.width.height.offset(kwt(18));
    }];
    
    
    
    UIButton * jsBtn = [UIButton buttonWithType:0];
    self.jsBtn = jsBtn;
    jsBtn.addTarget(self,@selector(jsBtnClick));
    jsBtn.addNormalText(@"接收").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [topview addSubview:jsBtn];
    [jsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.equalTo(zjBtn.mas_right);
        make.width.offset(kwt(80));
    }];
    UILabel * jslab = [DTKTool labelWithText:@"0" andFont:kCFont(10) andTextColor:UIColor.whiteColor];
    self.jslab = jslab;
    jslab.backgroundColor = KcolorHex(@"#FE5402");
    jslab.textAlignment = NSTextAlignmentCenter;
    [DTKTool YJ:kwt(9) andView:jslab];
    [jsBtn addSubview:jslab];
    [jslab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(11));
        make.centerX.equalTo(jsBtn).offset(kwt(25));
        make.width.height.offset(kwt(18));
    }];
    self.zjlab.hidden = YES;
    self.jslab.hidden = YES;

    
    UIView * btnline = [UIView new];
    self.btnline = btnline;
    btnline.backgroundColor = KcolorHex(@"#4965FE");
    btnline.frame = CGRectMake(kwt(27), kwt(47), kwt(26), kwt(3));
    [topview addSubview:btnline];
    
    
    UIButton * SXBtn = [UIButton buttonWithType:0];
    self.SXBtn = SXBtn;
    SXBtn.addTarget(self,@selector(SXBtnClick));
    SXBtn.addNormalText(@"筛选").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [SXBtn setTitleColor:KcolorHex(@"#0967FF") forState:UIControlStateSelected];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen"] forState:UIControlStateNormal];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen2"] forState:UIControlStateSelected];
    [SXBtn setImageEdgeInsets:UIEdgeInsetsMake(0, kwt(50), 0, 0)];
    [SXBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, kwt(-40), 0, 0)];
    [topview addSubview:SXBtn];
    [SXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.offset(0);
        make.width.offset(kwt(80));
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#D8D8D8");
    [topview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.height.offset(0.5);
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(topview.mas_bottom);
    }];
    MJWeakSelf
    [self addFooterView:^{
        [weakSelf addMoreData];
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    
    
}

- (void)btnClickWithbtn:(UIButton *)btn{
    self.qbBtn.addFont(kFont(17));
    self.zjBtn.addFont(kFont(17));
    self.jsBtn.addFont(kFont(17));
    btn.addFont(kCFont(19));
    self.btnline.mj_x = btn.mj_x + kwt(27);
    
}
- (void)qbBtnClick{
    self.model.type = 0;
    [self btnClickWithbtn:self.qbBtn];
    [self refreshingData];
    
}
- (void)zjBtnClick{
    self.model.type = 2;
    [self btnClickWithbtn:self.zjBtn];
    [self refreshingData];
    
}
- (void)jsBtnClick{
    self.model.type = 1;
    [self btnClickWithbtn:self.jsBtn];
    [self refreshingData];
    
}
- (void)SXBtnClick{
    joinScreenViewController * vc = [joinScreenViewController new];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)refreshingData{
    self.pageNo = 1;
    [self.modelArray removeAllObjects];
    [self loadNewData];
}
- (void)jjslApi{

    [self.task cancel];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    self.task = [SCNetManger PostAuditGetHandoverCountModelWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code == 200){
            NSArray<AuditCountModel *> * timeArray = [NSArray yy_modelArrayWithClass:AuditCountModel.class json:model.result];
            NSLog(@"%@",timeArray);
            for (AuditCountModel * countModel in timeArray) {
                if([countModel.statusName isEqualToString:@"转交"]){
                    if(countModel.count > 0){
                        self.zjlab.hidden = NO;
                        self.zjlab.text = [NSString stringWithFormat:@"%d",countModel.count];
                    } else {
                        self.zjlab.hidden = YES;
                    }
                }
                if([countModel.statusName isEqualToString:@"接收"]){
                    if(countModel.count > 0){
                        self.jslab.hidden = NO;
                        self.jslab.text = [NSString stringWithFormat:@"%d",countModel.count];
                    } else {
                        self.jslab.hidden = YES;
                    }
                }
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)loadNewData {
    [self jjslApi];
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [self.model SSDict];
    [dict setObject:@(self.pageNo) forKey:@"pageNo"];
    [dict setObject:@(self.pageSize) forKey:@"pageSize"];
    //    if(![self.dict.allKeys containsObject:@"startTime"]){///没有开始时间
    //        [self.dict setObject:self.createTime forKey:@"createTime"];
    //    }
    
    [SCNetManger PostAuditGetHandoverListWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:joinQuestListModel.class json:model.result];
            [self.modelArray addObjectsFromArray:array];
            if(array.count == 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}


//
//- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
//    return kwt(100);
//}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * headerview = [UIView new];
    UIView * btnview = [UIView new];
    MJWeakSelf
    btnview.addClickBlock(^{
        [weakSelf openTime];
    });
    [headerview addSubview:btnview];
    btnview.backgroundColor= UIColor.whiteColor;
    [DTKTool YJ:kwt(4) andView:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(10));
        make.height.offset(kwt(31));
    }];
    UILabel * time = [DTKTool labelWithText:@"-" andFont:kFont(16) andTextColor:KcolorHex(@"#222222")];
    [btnview addSubview:time];
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(11));
    }];
    
    UIImageView * img = kimageView(@"arrow_down");
    [btnview addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(kwt(-11));
        make.width.offset(kwt(12));
        make.height.offset(kwt(7));
        make.left.equalTo(time.mas_right).offset(kwt(11));
    }];
    if(self.model.startTime.length > 0){
        if([self.model.startTime isEqualToString:self.model.endTime]){
            time.text = self.model.startTime;
        } else {
            time.text = [NSString stringWithFormat:@"%@~%@",self.model.startTime,self.model.endTime];
        }
    } else {
        time.text = self.model.createTime;
    }
    return headerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kwt(50);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.modelArray[indexPath.row].cellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    joinQuestTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"joinQuestTableViewCell"];
    if (cell == nil) {
        cell = [[joinQuestTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"joinQuestTableViewCell"];
    }
    cell.model = self.modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MJWeakSelf
    cell.openJSBlock = ^(joinQuestListModel * _Nonnull model) {
        [weakSelf AuditConfirmHandoverWithauditId:model.ID];
    };
    return cell ;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    joinQuestDetailsViewController * vc = [joinQuestDetailsViewController new];
    vc.auditId = self.modelArray[indexPath.row].ID;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)openTime{
    MJWeakSelf
    if(self.model.startTime.length > 0){
        [TimeFrameShowView showviewWithTitle:@"请选择任务时间" andStartTime:self.model.startTime andEndTime:self.model.endTime andRightBlock:^(NSString * _Nonnull StartTime, NSString * _Nonnull EndTime) {
            weakSelf.model.endTime = EndTime;
            weakSelf.model.startTime = StartTime;
            [weakSelf refreshingData];
        } andCZBlock:^{
            weakSelf.model.endTime = @"";
            weakSelf.model.startTime = @"";
            weakSelf.SXBtn.selected = !weakSelf.model.isNew;
            [weakSelf refreshingData];
        }];
    } else{
        
        [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYM title:@"请选择任务申请时间" selectValue:self.model.createTime minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
            weakSelf.model.createTime = selectValue;
            [weakSelf refreshingData];
        }];
        
    }
}
- (void)AuditConfirmHandoverWithauditId:(NSString *)auditId{
    [self.view showBusyHUD];
    [SCNetManger PostAuditConfirmHandoverWithauditId:auditId andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            if(model.message.length > 0){
                [self.view showWarning:model.message];
            } else {
                [self.view showWarning:@"接收成功"];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self refreshingData];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

@end

